package shared.player;

import java.io.Serializable;


/**
 * The Class Player
 *  - used to store a players information.
 */
public class Player implements Serializable{

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 3845355631200987993L;
	
	/** The x coordinate. */
	public int x;
	
	/** The y coordinate. */
	public int y;
	
	/** The old x coordinate (from last move). */
	public int oldx = 0;
	
	/** The old y coordinate (from last move). */
	public int oldy = 0;
	
	/** The players id. */
	public int playerId;
	
	/** The name of the player. */
	public String name;
	
	/** The games id. */
	public int gameId;
	
	/** The direction in which the player is moving (0 - down, 1 - up, 2 - left, 3 - right). */
	public int direction;
	
	/** A boolean stating whether the player is ready for another move. */
	public boolean readyForMove = true;
	
	/** A boolean stating whether the player has been disconnected. */
	public boolean disconnected = false;
	
	/**
	 * Instantiates a new player.
	 *
	 * @param name the name of the player
	 * @param x the starting x coordinate
	 * @param y the starting y coordinate
	 * @param direction the starting direction
	 * @param gameId the game id
	 * @param playerId the player id
	 */
	public Player(String name, int x, int y, int direction, int gameId, int playerId){
		this.x = x;
		this.y = y;
		this.direction = direction;
		this.name = name;
		this.gameId = gameId;
		this.playerId = playerId;
	}
	
	/**
	 * Sets the direction of the player.
	 *
	 * @param dir the new direction
	 */
	public void setDir(int dir){
		direction = dir;
	}
	
	/**
	 * Move - moves to the next coordinates on the board
	 * specified by the players direction.
	 *
	 * @param board the game board
	 */
	public void move(Integer[][] board){
		oldx = x;
		oldy = y;
		if (direction == 0) {
			y --;
		} else if (direction == 1) {
			y ++;
		} else if (direction == 2) {
			x --;
		} else if (direction == 3) {
			x ++;
		}
		//lay down a trail
		synchronized(board){
			board[oldx][oldy] = 1;
			if(!disconnected){
				readyForMove = false;
			}else{
				readyForMove = true;
			}
		}
	}
	
	
	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Player other = (Player) obj;
		if (gameId != other.gameId) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (playerId != other.playerId) {
			return false;
		}
		return true;
	}
	
	
	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + gameId;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + playerId;
		return result;
	}
}
